﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class FrmLoading : Form
    {
        //保存父窗口信息，主要用于居中显示加载窗体
        private Form partentForm = null;

        private string strMsg = "正在加载中，请稍候";
        private string strPoint = "";

        public FrmLoading(Form partentForm)
        {
            InitializeComponent();
            this.partentForm = partentForm;
        }

        public void SetMsg(string msg)
        {
            if (!string.IsNullOrWhiteSpace(msg))
                this.lblInfo.Invoke((new MethodInvoker(() =>
                {
                    this.lblInfo.Text = strMsg;
                })));
            this.timer1.Enabled = true;
        }

        private void SetText()
        {
            if (this.strPoint.Length > 5)
                this.strPoint = "";
            else
                this.strPoint += ".";

            this.lblInfo.Invoke((new MethodInvoker(() =>
            {
                this.lblInfo.Text = this.strMsg + this.strPoint;
            })));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.SetText();
        }

        private void FrmLoading_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.timer1.Enabled = false;
        }

        private void FrmLoading_Load(object sender, EventArgs e)
        {
            this.Opacity = 0.9d;
            //设置一些Loading窗体信息
            this.ControlBox = false;
            int clac = 50;
            // 下面的方法用来使得Loading窗体居中父窗体显示
            int parentForm_Position_x = this.partentForm.Location.X;
            int parentForm_Position_y = this.partentForm.Location.Y;
            int parentForm_Width = this.partentForm.Width;
            int parentForm_Height = this.partentForm.Height;
            this.Width = parentForm_Width - clac;
            this.Height = parentForm_Height - clac;

            int start_x = (int)(parentForm_Position_x + clac / 2);
            int start_y = (int)(parentForm_Position_y + clac / 2);
            this.Location = new System.Drawing.Point(start_x, start_y);
            if (this.panel1.Width > this.Width)//小屏展示
            {
                this.panel1.Width = (int)(this.Width * 0.8);
                this.panel1.Location = new Point((int)(this.Width * 0.1), this.panel1.Location.Y);
                this.lblInfo.Width = this.panel1.Width > 150 ? 150 : this.panel1.Width;
                this.lblInfo.Location = new Point((int)((this.panel1.Width - this.lblInfo.Width) * 0.5), 0);
            }
        }
    }
}